
class Widget extends MovieClip
{
	public var title:TextField;
	public var clock:TextField;
	
	public var canvas:MovieClip;
	public var refresh:MovieClip;
	
	public var offset:Number;
	
	public var users:Object;
	public var userClips:Array;
	
	public var frame:Number;
	
	public function reload ()
	{
		if (this.userClips != undefined)
		{
			for (var i = 0; i < this.userClips.length; i++)
			{
				this.userClips[i].removeMovieClip ();
			}
		}
		
		var me = this;
		me.users = new Object ();
		
		var xml:XML = new XML ();
		
		xml.onLoad = function (success:Boolean)
		{
			var group:XMLNode = this.firstChild;

			me.title.text = group.attributes.name;
			
			var xml_users:Array = group.childNodes;
			
			for (var i = 0; i < xml_users.length; i++)
			{
				var u:XMLNode = xml_users[i];
				
				if (u.nodeName == "user")
				{
					var name:String = u.attributes.name;
					var date:String = u.attributes.date;
					var field:String = u.attributes.field;
					var value:String = u.attributes.value;
					var icon:String = u.attributes.icon;

					var user:Object = me.users[name];
				
					if (user == undefined)
					{
						user = new Object ();	
						me.users[name] = user;
					}
				
					user["name"] = name;
					user["date"] = date;
					user["icon"] = icon;
					user[field] = value;
					user["order"] = i;
				}
			}
			
			me.display ();
		}
		
		// xml.load ("group.xml");
		xml.load ("http://www.aetherial.net/context-macosx/genkanban/online/cgi-bin/group.cgi");
	}
	
	public function display ()
	{
		this.userClips = new Array ();

		var now:Date = new Date ();

		var i:Number = 0;
		
		var sortedUsers = new Array ();

		for (var name:String in this.users)
		{
			var user:Object = this.users[name];
			sortedUsers.push (user);
		}
		
		sortedUsers = sortedUsers.sortOn ("order", Array.DESCENDING);
		
		
		for (var i = 0; i < sortedUsers.length; i++)
		{
			var user:Object = sortedUsers[i];

			var clip:MovieClip = this.canvas.attachMovie ("Person", "person" + i, 
															this.canvas.getNextHighestDepth ());
			this.userClips.push (clip);
			
			clip.name.text = user["name"];
			
			if (user["Activity"] != undefined)
				clip.activity.text = user["Activity"];
			else
				clip.activity.text = "Unknown activity";

			if (user["Location"] != undefined)
				clip.location.text = user["Location"];
			else
				clip.location.text = "Unknown location";

			if (user["Social Context"] != undefined)
				clip.social.text = user["Social Context"];
			else
				clip.social.text = "Unknown social context";

			var dateArray = user["date"].split (" ");
			var dayArray = dateArray[0].split ("-");
			var timeArray = dateArray[1].split (":");

			var date:Date = new Date (dayArray[0], (dayArray[1] - 1), dayArray[2], timeArray[0], timeArray[1], timeArray[2]);

			var ago:Number = Math.round ((now.getTime () - date.getTime ()) / 60000);

			/* var text:String = "Updated " + ago + " minutes ago";
			
			if (ago == 1)
				text = "Updated " + ago + " minute ago";
			else if (ago == 0)
				text = "Updated less than a minute ago"; */
				
			var text:String = "Last update: " + dateArray[1];

			clip.date.text = text;
				
			clip._y = 10 + (i * 96);
			clip._x = 10;
			
			clip.user = user;
			
			clip.onEnterFrame = function ()
			{
				var mcl:MovieClipLoader = new MovieClipLoader (); 

				mcl.addListener (this); 
				mcl.loadClip (this.user.icon, this.image);
				
				this.onLoadComplete = function (mc:MovieClip) 
				{ 
					mc.onEnterFrame = function ()
					{
						this._height = 70;
						this._width = 70;
						
						this._x = 5;
						this._y = 5;
					}
				} 

				
				this.onEnterFrame = undefined;
			}
		}
		
		this.onMouseDown = function ()
		{
			this.mouseStart = this._ymouse;
		}
		
		this.onMouseUp = function ()
		{
			this.mouseStart = undefined;
		}
		
		this.onMouseMove = function ()
		{
			if (this.mouseStart != undefined)
			{
				var currentMouse:Number = this._ymouse;
				
				var newY:Number = this.canvas._y - (this.mouseStart - currentMouse);
			
				if (newY + canvas._height >= 194 && newY <= 26)
					this.canvas._y = newY;
				
				this.mouseStart = currentMouse;
			}
		}
		
		var me = this;
		
		this.refresh.onPress = function ()
		{
			me.reload ();
		}
	}
	
	public function onEnterFrame ()
	{
		this.reload ();
		
		this.frame = 1;
		
		this.onEnterFrame = function ()
		{
			if (this.frame % 12 == 0)
			{
				var now:Date = new Date ();
				
				var hours:Number = now.getHours ();
				var minutes:Number = now.getMinutes ();
				
				var dateString:String = (hours % 12) + ":";
				
				if ((hours % 12) == 0)
					dateString = "12:";
				if (minutes < 10)
					dateString += "0"
					
				dateString += minutes + " ";
				
				if (hours >= 12)
					dateString += "PM";
				else
					dateString += "AM";
					
				if (this.time.text != dateString)
					this.time.text = dateString;

				switch (now.getMonth ())
				{
					case 0: dateString = "Jan. "; break;
					case 1: dateString = "Feb. "; break;
					case 2: dateString = "Mar. "; break;
					case 3: dateString = "Apr. "; break;
					case 4: dateString = "May "; break;
					case 5: dateString = "June "; break;
					case 6: dateString = "July "; break;
					case 7: dateString = "Aug. "; break;
					case 8: dateString = "Sep. "; break;
					case 9: dateString = "Oct. "; break;
					case 10: dateString = "Nov. "; break;
					case 11: dateString = "Dec. "; break;
				}
				
				dateString += (now.getDate () + 0) + ", ";
				dateString += " " + now.getFullYear ();

				if (this.date.text != dateString)
					this.date.text = dateString;
				

				this.frame = 0;
			}
		
			this.frame += 1;
		}
	}
}

